'use strict';

angular.module('sfhtHaitaoCmsApp')
    .service('CommonBiz', function($q, Gateway, $injector, Notification) {

        window.alert =function(message){
            Notification.clearAll();
            Notification.error({message: message, replaceMessage: true});
        }

        // 通用的request方法返回promise对象
        var request = function(api, params, isForceUserLogin, cms) {

            var deferred = $q.defer();

            var api = $injector.get(api);

            var gateway = new Gateway(api, params);

            gateway.sendRequest(false, true)
                .then(function(data) {
                    deferred.resolve(data);
                }, function(data) {
                    deferred.reject(data);
                });

            return deferred.promise;
        };

        var errorCallback = function(data) {
            alert("Error: " + data);
        };
        return {
            //模块数据的操作
            'deleteData': function($scope, item) {
                var params = {
                    id: item.id,
                    version: item.version,
                    listId: item.listId
                }
                return request('sf_b2c_mall_api_cms_deleteCmsData', params, false, true);
            },

            'getDataList': function(pageId, placeHolder) {
                var params = {
                    pageId: pageId,
                    placeHolder: placeHolder
                }
                return request('sf_b2c_mall_api_cms_getDataList', params, false, true);
            },

            'getModuleDetail': function(name, type, needAll) {
                var params = {
                    moduleName: name,
                    moduleType: type,
                    needAll: false
                }
                return request('sf_b2c_mall_api_cms_getModuleDetail', params, false, true);
            },

            'getNations': function($scope) {
                var params = {}
                return request('sf_b2c_mall_api_cms_getNations', params, false, true);
            },

            'updateSingleCmsData': function(record) {
                _.each(record.properties, function(item) {
                    if (item.field.fieldType == 'Date') {
                        item.propertyValue = new Date(item.propertyValue).getTime();
                    }
                });
                var data = {
                    id: record.id,
                    listId: record.listId,
                    itemId: record.itemId,
                    version: record.version,
                    properties: record.properties
                };
                var params = {
                    data: JSON.stringify(data)
                }
                return request('sf_b2c_mall_api_cms_updateCmsData', params, false, true);
            },
            'bantchUpdateCmsData': function(records) {
                _.each(records, function(record) {
                    _.each(record.properties, function(item) {
                        if (item.field.fieldType == 'Date') {
                            item.propertyValue = new Date(item.propertyValue).getTime();
                        }
                    });
                });
                var params = {
                    datas: JSON.stringify(records)
                }
                return request('sf_b2c_mall_api_cms_bantchUpdateData', params, true, true);
            },

            'updateAllCmsData': function(mainproducts) {
                var data = [];
                if (mainproducts && mainproducts.length > 0) {
                    data = mainproducts;
                } else {
                    data[0] = mainproducts;
                }

                var params = {
                    dataList: JSON.stringify(data)
                }
                return request('sf_b2c_mall_api_cms_recordDataMove', params, false, true);
            },

            //子模块的操作
            'getSubModuleList': function(moduleId) {
                var params = {
                    moduleId: moduleId
                }
                return request('sf_b2c_mall_api_cms_getSubModuleList', params, false, true);
            },

            'getAllFields': function(moduleId) {
                var params = {}
                return request('sf_b2c_mall_api_cms_getAllFields', params, false, true);
            },

            'getDataProcessorList': function() {
                var params = {}
                return request('sf_b2c_mall_api_cms_getDataProcessorList', params, false, true);
            },

            'createSubModule': function(subModule) {
                var params = {
                    submodule: JSON.stringify(subModule)
                }
                return request('sf_b2c_mall_api_cms_createSubModule', params, false, true);
            },

            'updateSubModule': function(subModule) {
                var params = {
                    submodule: JSON.stringify(subModule)
                }
                return request('sf_b2c_mall_api_cms_updateSubModule', params, false, true);
            },

            'bantchDeleteCmsData': function(datas) {
                var params = {
                    'datas': JSON.stringify(datas)
                }
                return request('sf_b2c_mall_api_cms_bantchDeleteCmsData', params, false, true);
            },

            //html模块的操作
            //check
            'addCmsData': function(dataList, addToEnd) {
                _.each(dataList, function(record) {
                    _.each(record.properties, function(item) {
                        if (item.propertyName == 'startTime' || item.propertyName == 'endTime') {
                            if (item.propertyValue == 0) {
                                item.propertyValue = new Date().getTime();
                            } else {
                                item.propertyValue = new Date(item.propertyValue).getTime();
                            }
                        }
                    });
                });
                var params = {
                    dataList: JSON.stringify(dataList),
                    addToEnd: addToEnd
                }
                return request('sf_b2c_mall_api_cms_addCmsData', params, false, true);
            },

            'getFieldOfSubModule': function(subModuleId) {
                var params = {
                    submoduleId: subModuleId
                }
                return request('sf_b2c_mall_api_cms_getFieldOfSubModule', params, false, true);
            },

            //模块管理
            'updateModuleDraft': function(module) {
                var params = {
                    cmsModule: JSON.stringify(module)
                }
                return request('sf_b2c_mall_api_cms_updateModuleDraft', params, false, true);
            },

            'createModuleDraft': function(module) {
                var params = {
                    cmsModule: JSON.stringify(module)
                }
                return request('sf_b2c_mall_api_cms_createModuleDraft', params, false, true);
            },

            'getItems': function(params) {
                return request('sf_b2c_mall_api_cms_getItems', params, false, true);
            },

            'cloneDataList': function(orgListId, addToEnd, targetListId) {
                var params = {
                    orgListId: orgListId,
                    targetListId: targetListId,
                    addToEnd: addToEnd
                }
                return request('sf_b2c_mall_api_cms_cloneDataList', params, false, true);
            },

            'replaceDataList': function(orgListId, targetListId) {
                var params = {
                    orgListId: orgListId,
                    targetListId: targetListId
                }
                return request('sf_b2c_mall_api_cms_replaceDataList', params, false, true);
            },

            'shareDataList': function(orgListId, targetListId, pageId, placeHolder) {
                var params = {
                    orgListId: orgListId,
                    pageId: pageId,
                    placeHolder: placeHolder,
                    targetListId: targetListId
                }
                return request('sf_b2c_mall_api_cms_shareDataList', params, false, true);
            },

            'searchItemAggregation': function(searchKeywords) {
                var params = {
                    'searchKeywords': searchKeywords
                }
                return request('sf_b2c_mall_api_cms_searchItemAggregation', params, false, true);
            },

            'getResourceVersion': function() {
                var params = {

                }
                return request('sf_b2c_mall_api_cms_getResourceVersion', params, false, true);
            },

            'updateResourceVersion': function(artifact, version) {
                var params = {
                    'artifact': artifact,
                    'version': version
                }
                return request('sf_b2c_mall_api_cms_updateResourceVersion', params, false, true);
            },

            'getUserProfile': function() {
                var params = {

                }
                return request('sf_b2c_mall_api_cms_getUserProfile', params, false, true);
            },
            'publishData': function(pageId, placeHolder) {
                var params = {
                    pageId: pageId,
                    placeHolder: placeHolder
                }
                return request('sf_b2c_mall_api_cms_publishData', params, false, true);
            },
            'revert': function(pageId, placeHolder) {
                var params = {
                    pageId: pageId,
                    placeHolder: placeHolder
                }
                return request('sf_b2c_mall_api_cms_revert', params, false, true);
            },
            'getAccessURL': function(pageId) {
                var params = {
                    pageId: pageId
                }
                return request('sf_b2c_mall_api_cms_getAccessURL', params, false, true);
            },
            // 'smartSort': function(cmsDatas, weightConfig) {
            //     var params = {
            //         cmsDatas: JSON.stringify(cmsDatas),
            //         weightConfig: JSON.stringify(weightConfig)
            //     }
            //     return request('sf_b2c_mall_api_cms_smartSort', params, false, true);
            // },
            // 'getWeightConfigFields': function() {
            //     return request('sf_b2c_mall_api_cms_getWeightConfigFields', {}, false, true);
            // },
            // 'getAccountStocktakingRules': function() {
            //     return request('sf_b2c_mall_api_cms_getAccountStocktakingRules', {}, true, true);
            // },
            // 'importStocktakingRules': function(idList) {
            //     var params = {
            //         idList: idList
            //     }
            //     return request('sf_b2c_mall_api_cms_importStocktakingRules', params, true, true);
            // },
            // 'deleteAccountStocktakingRule': function(id) {
            //     var params = {
            //         id: id
            //     }
            //     return request('sf_b2c_mall_api_cms_deleteAccountStocktakingRule', params, true, true);
            // }
        }
    });
